using System;
using System.Collections.Generic;
using System.Text;
using Alogient.Cameleon.Forms.Sdk;
using Alogient.Cameleon.Forms.Sdk.Items;
using System.Web.UI.WebControls;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    public class CMFormSummary : Table
    {
        public CMFormSummary(Submission submission)
        {
            if (submission != null)
            {
                TableRow headerRow = new TableRow();

                TableCell headerCell = new TableCell();
                headerCell.ColumnSpan = 2;

                Literal literal = new Literal();
                literal.Text = Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetLabelsRessource("Summary");

                headerCell.Controls.Add(literal);

                headerRow.Cells.Add(headerCell);

                this.Rows.Add(headerRow);
                
                foreach (SubmissionValue value in submission.values)
                {
                    TableRow row = new TableRow();
                    TableCell labelCell = new TableCell();
                    TableCell valueCell = new TableCell();

                    Literal labelLiteral = new Literal();
                    Literal valueLiteral = new Literal();

                    labelLiteral.Text = value.FormElement(submission.CultureCode).Label;
                    valueLiteral.Text = CleanValue(value);

                    labelCell.Controls.Add(labelLiteral);
                    valueCell.Controls.Add(valueLiteral);

                    row.Cells.Add(labelCell);
                    row.Cells.Add(valueCell);

                    this.Rows.Add(row);
                }
            }
        }

        protected string CleanValue(SubmissionValue value)
        {
            FormElement element = value.FormElement(value.Submission.CultureCode);
           
            switch (element.InputType)
            {
                case FormInputType.Checkbox:
                case FormInputType.Select:
                    {
                        StringBuilder sb = new StringBuilder();
                        string[] values = value.Value.Split(';');

                        foreach (string s in values)
                        {
                            if (!string.IsNullOrEmpty(s))
                                sb.AppendFormat("{0}, ", GetOptionName(element, s));
                        }
                        if (sb.Length > 2)
                            sb.Remove(sb.Length - 2, 1);
                        return sb.ToString();
                    }
                case FormInputType.Radio:
                    {
                        try
                        {
                            return GetOptionName(element, new FormElementChoice(Convert.ToInt32(value.Value), value.Submission.CultureCode).Value);
                        }
                        catch { return value.Value; }
                    }
                default:
                    { return value.Value; } 
            }
        }

        protected string GetOptionName(FormElement element, string value)
        {
            foreach (FormElementChoice choice in element.Choices)
            {
                if (choice.ID.ToString() == value)
                {
                    return choice.Name;
                }
            }

            return value;
        }
    }
}
